package com.controller.system;

import com.common.core.controller.BaseController;
import com.common.utils.AjaxResult;
import com.common.utils.PageUtils;
import com.system.entity.SysUserRoleEntity;
import com.system.entity.utilentity.RoleVo;
import com.system.service.ISysRoleService;
import com.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户和角色关联表
 *
 * @author yangzhan
 * @email *****@mail.com
 * @date 2022-04-11 20:41:31
 */
@Controller
@RequestMapping("/sysUserRole")
public class SysUserRoleController extends BaseController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysRoleService sysRoleService;


    /**
     * 分配用户
     */
    @GetMapping("/assign")
    public String sysUserRole(@RequestParam("id") Long id, Model model) {
        model.addAttribute("role", sysRoleService.sysRoleGetById(id));
        return "system/sysrole/assign" ;
    }

    /**
     * 用户和角色关联表 添加
     *
     * @param sysUserRoleEntity
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody SysUserRoleEntity sysUserRoleEntity) {

        return sysUserRoleService.sysUserRoleAdd(sysUserRoleEntity);
    }

    /**
     * 用户和角色关联表
     *
     * @param ids
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody List<Integer> ids) {
        return sysUserRoleService.sysUserRoleDel(ids);
    }

    /**
     * @param sysUserRoleEntity 更新数据
     */
    @PutMapping("/update")
    @ResponseBody
    public AjaxResult update(@RequestBody SysUserRoleEntity sysUserRoleEntity) {
        return sysUserRoleService.sysUserRoleUpById(sysUserRoleEntity);
    }

    /**
     * 数据查询
     */
    @GetMapping("/listByPage")
    @ResponseBody
    public AjaxResult findListByPage(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestParam("id") Integer id) {
        SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
        sysUserRoleEntity.setPage(PageUtils.pageUtils(page, limit));
        sysUserRoleEntity.setUserId(id.longValue());
        sysUserRoleEntity.setLimit(limit);
        return sysUserRoleService.sysUserRoleSelectListByPage(sysUserRoleEntity);
    }

    /**
     * 数据查询
     */
    @GetMapping("/listByPageTo")
    @ResponseBody
    public AjaxResult findListByPageTo(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
        sysUserRoleEntity.setPage(PageUtils.pageUtils(page, limit));
        sysUserRoleEntity.setLimit(limit);
        return sysUserRoleService.sysUserRoleSelectListByPageTo(sysUserRoleEntity);
    }


    /**
     * 添加分配的角色
     *
     * @param roleVo
     * @return
     */
    @PostMapping("/addRole")
    @ResponseBody
    public AjaxResult addRole(@RequestBody RoleVo roleVo) {
        return sysUserRoleService.addRole(roleVo);
    }


}
